FUNCTION(ADD_TESTS_FROM target)
	GET_PROPERTY(target_sources TARGET ${target} PROPERTY SOURCES)
	
	# Process each file looking for declared tests
	SET(declarations "")
	SET(map_cpp "")
	SET(map_c "")
	FOREACH(arg ${target_sources})
		# Ignore header files
		IF("${arg}" MATCHES "\\.h$")
			CONTINUE()
		ENDIF()
		# Read in the file
		FILE(READ ${arg} raw_source)
		STRING(REGEX REPLACE ";" "\\\\;" raw_source "${raw_source}")
		STRING(REGEX REPLACE "\n" ";" raw_source "${raw_source}")
		# Scan each line
		FOREACH(line ${raw_source})
			IF("${line}" MATCHES "SBDF_TEST_C[ \t]*\\(([A-Za-z0-9_.]+),[ \t]*([A-Za-z0-9_.]+)")
				# SBDF_TEST_C() macro
				SET(test_category "${CMAKE_MATCH_1}")
				SET(test_name "${CMAKE_MATCH_2}")
				ADD_TEST(NAME "${test_category}.${test_name}" COMMAND ${target} "${test_category}.${test_name}")
				SET(declarations "${declarations}extern \"C\" int test_c_${test_category}_${test_name}(char *buf, int buf_len);\n")
				SET(map_c "${map_c}  {\"${test_category}.${test_name}\", test_c_${test_category}_${test_name}},\n")
			ELSEIF("${line}" MATCHES "SBDF_TEST[ \t]*\\(([A-Za-z0-9_.]+),[ \t]*([A-Za-z0-9_.]+)")
				# SBDF_TEST() macro
				SET(test_category "${CMAKE_MATCH_1}")
				SET(test_name "${CMAKE_MATCH_2}")
				ADD_TEST(NAME "${test_category}.${test_name}" COMMAND ${target} "${test_category}.${test_name}")
				SET(declarations "${declarations}void test_${test_category}_${test_name}();\n")
				SET(map_cpp "${map_cpp}  {\"${test_category}.${test_name}\", test_${test_category}_${test_name}},\n")
			ENDIF()
		ENDFOREACH()
	ENDFOREACH()

	# Output the header
	SET(header "/* This file is generated by CMake.  Do not modify. */\n\n#include <map>\n\n${declarations}\ntypedef std::map<const char*, void(*)()> test_list_cpp;\ntest_list_cpp list1 = {\n${map_cpp}};\ntypedef std::map<const char*, int(*)(char *, int)> test_list_c;\ntest_list_c list2 = {\n${map_c}};\n")
	FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${target}_tests.h" "${header}")
	TARGET_SOURCES(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${target}_tests.h")
ENDFUNCTION()
